package com.zjxf.base.bean.annotation;

import com.zjxf.base.common.SysConst;
import org.apache.commons.lang3.StringUtils;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.*;

/**
 * Created with IntelliJ IDEA.
 * author: create by ZhangHao
 * Date: 2019/6/20
 * Time: 10:35
 */
@Documented
@Constraint(validatedBy = BaseCode.Validator.class)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface BaseCode {

    String message() default "不规范的base64Code码";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    class Validator implements ConstraintValidator<BaseCode, String> {
        @Override
        public boolean isValid(String baseCodeStr, ConstraintValidatorContext constraintValidatorContext) {
            if (StringUtils.isBlank(baseCodeStr)) {
                return Boolean.TRUE;
            }
            return baseCodeStr.contains(SysConst.SPLIT_TAG);
        }
    }
}
